﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;

namespace RemotePrintTimer
{
    class SqlLiteUtils
    {

        public void createDb()
        {
            string connectionString = "data source=" + GValues.dbpath;
            SQLiteConnection con = new SQLiteConnection(connectionString);
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand(con);
            cmd.CommandText = "DROP TABLE IF EXISTS user;CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT,account NVARCHAR (50), password NVARCHAR (50),domainid NVARCHAR (36),token  NVARCHAR (2000));";
            cmd.ExecuteNonQuery();
            //cmd.CommandText = "insert into user (account,password) values ('acc01','123456'); insert into user (account,password) values ('acc02','123456');";
            //cmd.ExecuteNonQuery();
            con.Close();

        }
        public void insertData(String account, String password, String domainid, String token)
        {
            string connectionString = "data source=" + GValues.dbpath; ;
            SQLiteConnection con = new SQLiteConnection(connectionString);
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand(con);
            //cmd.CommandText = "DROP TABLE IF EXISTS user;CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT,account NVARCHAR (50), password NVARCHAR (50));";
            //cmd.ExecuteNonQuery();
            cmd.CommandText = "insert into user (account,password,domainid,token) values ('" + account + "','" + password + "','" + domainid + "','" + token + "');";
            cmd.ExecuteNonQuery();
            con.Close();

        }
        //public void updateData(String account, String password, String domainid, String token)
        //{
        //    string connectionString = "data source=" + GValues.dbpath; ;
        //    SQLiteConnection con = new SQLiteConnection(connectionString);
        //    con.Open();
        //    SQLiteCommand cmd = new SQLiteCommand(con);
        //    //cmd.CommandText = "DROP TABLE IF EXISTS user;CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT,account NVARCHAR (50), password NVARCHAR (50));";
        //    //cmd.ExecuteNonQuery();
        //    cmd.CommandText = "update user set token = " + token + " where account = " + account + ";";
        //    cmd.ExecuteNonQuery();
        //    con.Close();
        //}

        public void updateData(String commandString)
        {
            string connectionString = "data source=" + GValues.dbpath; ;
            SQLiteConnection con = new SQLiteConnection(connectionString);
            con.Open();
            SQLiteCommand cmd = new SQLiteCommand(con);
            //cmd.CommandText = "DROP TABLE IF EXISTS user;CREATE TABLE student (id INTEGER PRIMARY KEY AUTOINCREMENT,account NVARCHAR (50), password NVARCHAR (50));";
            //cmd.ExecuteNonQuery();
            cmd.CommandText = commandString;
            cmd.ExecuteNonQuery();
            con.Close();
        }
        class User
        {
            public Int64 id { get; set; }
            public String account { get; set; }
            public String password { get; set; }
        }


    }
}
